package com.now.printer.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.gamerole.orcameralib.CameraActivity;
import com.now.printer.ui.activity.FileBrowerManagerActivity;
import com.now.printer.ui.activity.FilePreviewAndPrintActivity;
import com.now.printer.ui.activity.HtmlPrintActivity;
import com.now.printer.ui.activity.MainActivity;
import com.now.printer.R;
import com.now.printer.base.BaseActivity2;
import com.now.printer.base.BaseFragment2;
import com.now.printer.ui.activity.PrintHistoryRecordActivity;
import com.now.printer.ui.view.SimpleViewPagerIndicator;
import com.now.printer.utils.FileUtils;

import org.jetbrains.annotations.NotNull;
import org.xutils.common.util.LogUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.Objects;

import static android.app.Activity.RESULT_OK;

public class MyContainerFragment extends BaseFragment2  implements View.OnClickListener{

    private SimpleViewPagerIndicator mIndicator;
    private ViewPager mViewPager;

    private TextView tv_html_print, btn_file_brower, btn_print_record,btnIdCard;

    @Override
    protected void onLazy() {

    }

    public int getLayoutId() {
        return R.layout.fragment_container_my;
    }

    @Override
    public void doBusiness(Context mContext, View convertView) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setToolbar();
        }

        mIndicator = (SimpleViewPagerIndicator) convertView.findViewById(R.id.id_stickynavlayout_indicator);
        mIndicator.setIndicatorColor(getResources().getColor(R.color.color_264DFF));
        mViewPager = (ViewPager) convertView.findViewById(R.id.id_stickynavlayout_viewpager);

        btn_file_brower = convertView.findViewById(R.id.btn_file_brower);
        btn_file_brower.setOnClickListener(this);
        btn_print_record = convertView.findViewById(R.id.btn_print_record);
        btn_print_record.setOnClickListener(this);

        btnIdCard = convertView.findViewById(R.id.btn_idcard);
        btnIdCard.setOnClickListener(this);

        tv_html_print = convertView.findViewById(R.id.tv_html_print);
        tv_html_print.setOnClickListener(this);

        initDatas();
        initEvents();
    }

    private String[] mTitles = new String[]{"最近", "收藏"};
    private Fragment[] mFragments = new Fragment[mTitles.length];
    private FragmentPagerAdapter mAdapter;

    private void initDatas() {
        mIndicator.setTitles(mTitles);

        mIndicator.setOnClickListener((SimpleViewPagerIndicator.OnclickListener) position -> {
            mViewPager.setCurrentItem(position);
        });

        for (int i = 0; i < mTitles.length; i++) {
            mFragments[i] = (Fragment) RecentlyFileFragment.newInstance(i + 1);
        }
//            mFragments[0] = (Fragment) RecentlyFileFragment.newInstance(mTitles[0]);
//           mFragments[1] = (Fragment) CollectedFileFragment.newInstance(mTitles[1]);

        mAdapter = new FragmentPagerAdapter(getFragmentManager()) {
            @Override
            public int getCount() {
                return mTitles.length;
            }

            @NotNull
            @Override
            public Fragment getItem(int position) {
                return mFragments[position];
            }
        };

        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(0);
//        mViewPager.setOffscreenPageLimit(2);
    }


    private void initEvents() {
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
            }

            @Override
            public void onPageScrolled(int position, float positionOffset,
                                       int positionOffsetPixels) {
                mIndicator.scroll(position, positionOffset);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    public MainActivity getContainer() {
        return (MainActivity) getActivity();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void setToolbar() {
        nbToolbar.setCenterTitle("我的");
        nbToolbar.setShowSearch();
        nbToolbar.setToolbarLeft(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((BaseActivity2) getActivity()).openMenu();
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_wenjian:
                Intent intent = new Intent();
                intent.putExtra("type", "1");//0是所有，1是文件浏览，2是图片
                intent.setClass(getActivity(), FileBrowerManagerActivity.class);
                getActivity().startActivity(intent);
                break;
            case R.id.tv_html_print:
                Intent intent2 = new Intent();
                intent2.setClass(getActivity(), HtmlPrintActivity.class);
                getActivity().startActivity(intent2);
                break;
            case R.id.btn_file_brower:
                Intent intent1 = new Intent(getActivity(), FileBrowerManagerActivity.class);
                intent1.putExtra("type", "1");//0是所有，1是文件浏览，2是图片
                getActivity().startActivity(intent1);
                break;
            case R.id.btn_print_record:
//                if (onTabListener != null) onTabListener.onSetTab();
                Intent intent_print_record = new Intent(getActivity(), PrintHistoryRecordActivity.class);
                startActivity(intent_print_record);
                break;
            case R.id.btn_idcard:
//                LibraryInitOCR.initOCR(getActivity());
//                Bundle bundle = new Bundle();
//                bundle.putBoolean("saveImage", true);
//                bundle.putInt("requestCode", SCAN_IDCARD_REQUEST);
//                bundle.putInt("type", 0); //0身份证, 1驾驶证
//                LibraryInitOCR.startScan(getActivity(), bundle);

//                Intent intent3 = new Intent(getActivity(), ScanIdCardActivity.class);
//                getActivity().startActivity(intent3);

//                Intent intent3 = new Intent(getActivity(), CameraActivity.class);
//                getActivity().startActivity(intent3);

                Intent intent5 = new Intent(getActivity(), CameraActivity.class);
                idCardFrontImgPath = FileUtils.getCurrentAppPath(Objects.requireNonNull(getActivity())) + File.separator  + "正面照"  + ".png";
                frontAndBackImagePath[0] = idCardFrontImgPath;

                intent5.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,idCardFrontImgPath);
                intent5.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_FRONT);
                startActivityForResult(intent5, REQUEST_CODE);
                break;
        }
    }

    private static final int REQUEST_CODE = 1001;
    private String[] frontAndBackImagePath = new String[2];
    private String idCardFrontImgPath = "";
    private String idCardBackImgPath = "";
    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null){
            String idCardFront = data.getStringExtra(CameraActivity.KEY_CONTENT_TYPE);

            if (TextUtils.equals(idCardFront,CameraActivity.CONTENT_TYPE_ID_CARD_FRONT)){
//                frontAndBackImagePath[0] = data.getStringExtra(CameraActivity.KEY_OUTPUT_FILE_PATH);

                Intent intent = new Intent(getActivity(), CameraActivity.class);
                idCardBackImgPath = FileUtils.getCurrentAppPath(Objects.requireNonNull(getActivity())) + File.separator + "反面照" + ".png";
                frontAndBackImagePath[1] = idCardBackImgPath;

                intent.putExtra(CameraActivity.KEY_OUTPUT_FILE_PATH,idCardBackImgPath);
                intent.putExtra(CameraActivity.KEY_CONTENT_TYPE, CameraActivity.CONTENT_TYPE_ID_CARD_BACK);
                startActivityForResult(intent, REQUEST_CODE);

            }else if (TextUtils.equals(idCardFront,CameraActivity.CONTENT_TYPE_ID_CARD_BACK)){
//
//                frontAndBackImagePath[1] = data.getStringExtra(CameraActivity.KEY_OUTPUT_FILE_PATH);
                LogUtil.i("current type id card front 正面照地址： " + frontAndBackImagePath[0]);
                LogUtil.i("current type id card back 反面照地址：" + frontAndBackImagePath[1]);

                Intent intent = new Intent(getActivity(), FilePreviewAndPrintActivity.class);
                intent.putExtra(FilePreviewAndPrintActivity.INTENT_KEY_ID_CARD_FRONT_BACK, frontAndBackImagePath);
//            ArrayList<String> pathList = data.getStringArrayListExtra(CameraActivity.INTENT_KEY_ID_CARD_FRONT_BACK);
                startActivity(intent);
            }
        }
    }
}
